package com.hptpd.questionnairesurvey.repository;

import com.hptpd.questionnairesurvey.domain.UserSurvey;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* Date: 2019/10/29
 * \*
 * \* Description:
 * \
 *
 * @author walter_long
 */
@Repository("userSurveyRep")
public interface UserSurveyRep extends JpaRepository<UserSurvey, String> {
    /**
     * 统计已完成未完成的调查
     * @param userId String
     * @return List<Map<String, Object>>
     */
    @Query(nativeQuery = true, value = "select is_done as isDone, count(is_done) as sum from user_survey where user_id = ? group by is_done")
    List<Map<String, Object>> countByUserIdAndSeperateIsDone(String userId);

    /**
     * 统计未完成的调查
     * @param userId String
     * @param isDone Boolean
     * @param state String
     * @return List<Map<String, Object>>
     */
    Integer countByUserIdAndIsDoneIsAndSurvey_StateIs(String userId, Boolean isDone, String state);

    /**
     * 统计已完成的调查
     * @param userId String
     * @param isDone Boolean
     * @return List<Map<String, Object>>
     */
    Integer countByUserIdAndIsDoneIs(String userId, Boolean isDone);

    /**
     * 分页列出用户调查
     * @param isDone Boolean
     * @param userId String
     * @param state String
     * @param pageable Pageable
     * @return Page<Survey>
     */
    Page<UserSurvey> findByIsDoneAndUser_IdIsAndSurvey_StateIsOrderBySurvey_CreatedAtDesc(Boolean isDone, String userId, String state, Pageable pageable);
    /**
     * 分页列出用户调查
     * @param isDone Boolean
     * @param userId String
     * @param pageable Pageable
     * @return Page<Survey>
     */
    Page<UserSurvey> findByIsDoneAndUser_IdIsOrderBySurvey_CreatedAtDesc(Boolean isDone, String userId, Pageable pageable);

    /**
     * 列出用户调查
     * @param isDone Boolean
     * @param surveyId String
     * @return List<UserSurvey>
     */
    List<UserSurvey> findByIsDoneAndSurvey_Id(Boolean isDone, String surveyId);
}
